package cn.echogo.framework.widget.imagezoom;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

public class PhotoPagerAdapter extends PagerAdapter {
	
	private static final String tag = PhotoPagerAdapter.class.getSimpleName();
	
	private ViewGroup[] pageArray;
	private Bitmap[] bitmapArray;
	
	private Context context;
	private int pageCount;
	private int defaultPhotoId;
	
	public PhotoPagerAdapter(Context context, int pageCount, int defaultPhotoId) {
		this.context = context;
		this.pageCount = pageCount;
		this.defaultPhotoId = defaultPhotoId;
		this.pageArray = new ViewGroup[pageCount];
		this.bitmapArray = new Bitmap[pageCount];
	}
	
	public void setPhotoBitmap(int position, Bitmap bitmap) {
		bitmapArray[position] = bitmap;
		ViewHolder holder = (ViewHolder) pageArray[position].getTag();
		holder.ivPhoto.setImageBitmap(bitmap);
		holder.progressBar.setVisibility(View.GONE);
	}
	
//	public Bitmap getPhotoBitmap(int position) {
//		return bitmapArray[position];
//	}
	
	public void resetScale(int position) {
		ViewHolder holder = (ViewHolder) pageArray[position].getTag();
		holder.ivPhoto.zoomTo(1f, 300);
	} 
	
	@Override
	public boolean isViewFromObject(View view, Object obj) {
		return view == obj;
	}

	@Override
	public int getCount() {
		return pageArray.length;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		Log.d(tag, "instantiateItem :: " + position);
		if(pageArray[position] == null) {
			pageArray[position] = new FrameLayout(context);
			//图片 
			ImageViewTouch ivPhoto = new ImageViewTouch(context);
			ivPhoto.setImageResource(defaultPhotoId);
			FrameLayout.LayoutParams prm = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);
			prm.gravity = Gravity.CENTER;
			pageArray[position].addView(ivPhoto, prm);
			//进度条
			ProgressBar progressBar = new ProgressBar(context);
			prm = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
			prm.gravity = Gravity.CENTER;
			pageArray[position].addView(progressBar, prm);
			
			ViewHolder holder = new ViewHolder();
			holder.ivPhoto = ivPhoto;
			holder.progressBar = progressBar;
			pageArray[position].setTag(holder);
		}
		ViewHolder holder = (ViewHolder) pageArray[position].getTag();
		if(bitmapArray[position] == null) {
			holder.progressBar.setVisibility(View.VISIBLE);
		} else {
			holder.progressBar.setVisibility(View.GONE);
			holder.ivPhoto.setImageBitmap(bitmapArray[position]);
		}
		container.addView(pageArray[position], 0);
		return pageArray[position];
	};

	public void destroyItem(ViewGroup container, int position, Object object) {
		Log.d(tag, "destroyItem :: " + position);
		container.removeView((View) object);
	};
	
	static class ViewHolder {
		ImageViewTouch ivPhoto;
		ProgressBar progressBar;
	}

}
